package antsclub.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import antsclub.common.EasyuiDataGridJson;
import antsclub.interceptor.ActivityInterceptor;
import antsclub.model.Activity;
import antsclub.model.UserActivity;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * ActivityController
 */
@Before(ActivityInterceptor.class)
public class ActivityController extends Controller {
	
	public void index() {
		String typeCode=getPara(0);
		typeCode="badminton";
		List<Activity> badmintonList=Activity.dao.find("select * from activity " +
				" where typeCode='"+typeCode+"' "+
				" order by createTime desc limit 3");
		typeCode="billiards";
		List<Activity> billiardsList=Activity.dao.find("select * from activity " +
				" where typeCode='"+typeCode+"' "+
				" order by createTime desc limit 3");
		typeCode="swimming";
		List<Activity> swimmingList=Activity.dao.find("select * from activity " +
				" where typeCode='"+typeCode+"' "+
				" order by createTime desc limit 3");
		//render("act_add.jsp");
		typeCode="other";
		List<Activity> otherList=Activity.dao.find("select * from activity " +
				" where typeCode='"+typeCode+"' "+
				" order by createTime desc limit 3");
		JSONObject json = new JSONObject();
		json.put("badmintonList", badmintonList);
		json.put("billiardsList", billiardsList);
		json.put("swimmingList", swimmingList);
		json.put("otherList", otherList);
		renderJson(JsonKit.toJson(json));
	}
	public void datagrid() {
		List<Activity> rows=Activity.dao.find("select * from activity order by id asc");
		EasyuiDataGridJson j=new EasyuiDataGridJson();
		j.setTotal(new Long(3));
		j.setRows(rows);
		renderJson(JsonKit.toJson(j));
	}
	public void add() {
	}
	
	//@Before(ActivityValidator.class)
	public void save() {
		try{
			Activity a=getModel(Activity.class);
			a.set("id", UUID.randomUUID().toString());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String currentTime=df.format(new Date());// new Date()为获取当前系统时间
			a.set("createTime", currentTime);
			a.set("updateTime", currentTime);
			boolean b=a.save();
			if(b){
				setAttr("msg", "活动发布成功");
				render("/index.jsp");
			}else{
				setAttr("msg", "活动发布失败");
				render("/activity/act_add.jsp");
			}
		}catch(Exception e){
			setAttr("msg", e);
			render("/activity/act_add.jsp");
		}
		
		
	}
	
	public void edit() {
		Activity a=Activity.dao.findById(getPara(0));
		renderJson(a);
	}
	public void view() {
		String actId=getPara(0);
		String sql="select a.*,ua.signName,ua.signNum,ua.userName from activity a "
					+" left join user_activity ua on a.id=ua.activityId "
					+" where a.id='"+actId+"' order by ua.signTime";
		List l=Db.find(sql);
		renderJson(l);
	}
	
	public void update() {
		getModel(Activity.class).update();
		setAttr("msg", "活动编辑成功");
		render("/index.jsp");
	}
	
	public void delete() {
		String[] ids= getPara().split(",");
		for(int i=0,len=ids.length;i<len;i++){
			Activity.dao.deleteById(ids[i]);
		}
		redirect("/activity");
	}
	
	//报名
	public void sign(){
		String actId=getPara("id");
		String uid=getSessionAttr("uid");
		String uname=getSessionAttr("uname");
		JSONObject json = new JSONObject();
		UserActivity ua=new UserActivity();
		Record r= Db.findFirst("select count(*) as num from " +
				" user_activity where activityId='"+actId+"' and" +
						" userId='"+uid+"'");
		if(r.getLong("num").intValue()>0){
			json.put("msg","这个活动您已经报过名了，不能重复报名");
			json.put("success", false);
		}else{
			ua.set("id", UUID.randomUUID().toString());
			ua.set("activityId", actId);
			ua.set("userId",uid);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String currentTime=df.format(new Date());// new Date()为获取当前系统时间
			ua.set("signTime", currentTime);
			ua.set("userName", uname);
			ua.set("signName", uname);
			ua.set("signNum", 1);
			ua.save();
			json.put("msg", "报名成功");
			json.put("success", true);
		}
		renderJson(JsonKit.toJson(json));
	}
	public void signMore(){
		String actId=getPara("id");
		String signName=getPara("signName");
		String signNum=getPara("signNum");
		String uid=getSessionAttr("uid");
		String uname=getSessionAttr("uname");
		JSONObject json = new JSONObject();
		UserActivity ua= UserActivity.dao.findFirst("select id from " +
				" user_activity where activityId='"+actId+"' and" +
						" userId='"+uid+"'");
		if(ua!=null){
			//已报名的情况下更新报名
			ua.set("signName", signName);
			ua.set("signNum", signNum);
			ua.update();
			ua=getSignInfo(actId);
			json.put("userActivity", ua);
			json.put("success", true);
		}else{
			//没有报名的情况下新增报名
			ua=new UserActivity();
			ua.set("id", UUID.randomUUID().toString());
			ua.set("activityId", actId);
			ua.set("userId",uid);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			String currentTime=df.format(new Date());// new Date()为获取当前系统时间
			ua.set("signName", signName);
			ua.set("signNum", signNum);
			ua.set("signTime", currentTime);
			ua.set("userName", uname);
			ua.save();
			ua=getSignInfo(actId);
			json.put("userActivity", ua);
			json.put("msg", "报名成功");
			json.put("success", true);
		}
		renderJson(JsonKit.toJson(json));
	}
	//取消报名
	public void undoSign(){
		String actId=getPara(0);
		String uid=getSessionAttr("uid");
		JSONObject json = new JSONObject();
		Record r= Db.findFirst("select count(*) as num from " +
				" user_activity where activityId='"+actId+"' and" +
						" userId='"+uid+"'");
		if(r.getLong("num").intValue()==0){
			json.put("msg","亲，这个活动你还没有报名呢");
			json.put("success", false);
		}else{
			Db.update("delete from user_activity where activityId='"+actId
					+"' and userId='"+uid+"'");
			UserActivity ua=getSignInfo(actId);
			json.put("userActivity", ua);
			json.put("msg", "报名取消成功");
			json.put("success", true);
		}
		renderJson(JsonKit.toJson(json));
	}
	
	public UserActivity getSignInfo(String actId){
		String sql="select signName,signNum from user_activity where activityId=? order by signTime";
		List<UserActivity> list=UserActivity.dao.find(sql,actId);
		String signName="";
		int signNum=0;
		UserActivity ua=new UserActivity();
		for(int i=0,len=list.size();i<len;i++){
			ua=list.get(i);
			if(ua.get("signNum")!=null){
				signNum+=ua.getInt("signNum");
			}else{
				signNum+=1;
			}
			if(ua.get("signName")!=null){
				signName+=", "+ua.getStr("signName");
			}
		}
		//有人报名的时候需要截取，因为最开始会是", "
		if(signName.length()>2){
			signName=signName.substring(2);
		}
		ua.set("signName", signName);
		ua.set("signNum", signNum);
		return ua;
	}
}


